package meshToSolid;

import grid.Grid3DFloat;
import hdgeom.primitives.Box;
import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PImage;
import simpleGui.Gui;
import simpleGui.GuiSpinnerGroup;

public class VoxelizationLayerViewer extends PApplet {

	
	Gui gui;
	int WHITE=color(255);
	int BLACK=color(0);
	GuiSpinnerGroup sliderY;
	int currentY=0;
	Grid3DFloat layer;
	Grid3DFloat gridHD;
	Box boundsScan;
	float dimGrid = 0.01f;//0.005f size of voxels in meters
	float dimGridHD = 0.003f;
	float mirrorX=0.03f;
	Box boundsCrop = new Box(-1.595f-mirrorX, -1.7534246f, 0.015989847f, 0.01f,
			1.4465753f, 2.17f);
	String pathValue;
	float[] xSegs;
	float[] zSegs;
	PFont myFont;
	String pathStream = "/Users/dillenburger_b/Documents/Projects/2014 Grotto2/hq/stream/";
	Part[][]parts;
	VoxelizationLayerManager layerManager;
	PImage img;
	float isoFactor = 0.85f;
	float iso = dimGridHD * isoFactor;
	public void setup() {
		// values20140618-114924_dim0.0050.txt
		pathValue = "/Users/dillenburger_b/Documents/Projects/2014 Grotto2/hq/values20140621-093522_dim0.0050.txt";
		size(1000, 600);
		constructScanBounds();
		createGridHD();
		setupGuiLayers();
		layerManager=new VoxelizationLayerManager(pathStream,gridHD,this);
		img=this.createImage(gridHD.nX,gridHD.nZ,RGB);
		initParts();
	}
	public void createGridHD() {
		gridHD = new Grid3DFloat();
		gridHD.nX = (int) (boundsScan.getDimX() / dimGridHD);
		gridHD.nY = (int) (boundsScan.getDimY() / dimGridHD);
		gridHD.nZ = (int) (boundsScan.getDimZ() / dimGridHD);
		gridHD.x1 = boundsScan.x1;
		gridHD.y1 = boundsScan.y1;
		gridHD.z1 = boundsScan.z1;
		gridHD.dim = dimGridHD;
	}
	public void initParts(){
		float dimX=0-boundsCrop.x1;
		int nX=(int)(dimX/0.3f);
		float realDX=dimX/nX;
		xSegs=new float[nX+1];
		for (int x=0;x<nX+1;x++){
			xSegs[x]=boundsCrop.x1+x*realDX;
		}
		
		float dimZ=boundsCrop.z2-boundsCrop.z1;
		int nZ=(int)(dimZ/0.3f);
		float realDZ=dimZ/nZ;
		zSegs=new float[nZ+1];
		for (int z=0;z<nZ+1;z++){
			zSegs[z]=boundsCrop.z1+z*realDZ;
		}
		
		parts=new Part[8][];
		for (int i=0;i<parts.length;i++){
		parts[i]=new Part[3];
		parts[i][0]=new Part(xSegs[3],zSegs[3],xSegs[5],zSegs[7]);
		parts[i][1]=new Part(xSegs[0],zSegs[3],xSegs[3],zSegs[7]);
		parts[i][2]=new Part(xSegs[0],zSegs[0],xSegs[5],zSegs[3]);
		}
	}
	public void constructScanBounds() {
		// bounds: x1:-2.22912 y1:-2.89137 z1:-0.11573 dX:2.35061 dY:1.6964
		// dZ:2.97742
		// new Box(-1.95f,-2.00f, -0.00f,0.02f,1.65f,2.84f);
		boundsScan = new Box();

		/*
		 * boundsVoxel.x1=-2.1f; boundsVoxel.y1=-2.2f; boundsVoxel.z1=-0.11573f;
		 * boundsVoxel.x2=0.05f; boundsVoxel.y2=1.75f; boundsVoxel.z2=3.05f;
		 */

		boundsScan.x1 = -1.6869289f;
		boundsScan.y1 = -1.9375342f;
		boundsScan.z1 = -0.100520186f;
		boundsScan.x2 = 0.047969546f;
		boundsScan.y2 = 1.5430137f;
		boundsScan.z2 = 2.5f;

		// -1.6869289,-1.9375342,-0.100520186
		// 0.047969546,1.5430137,2.4464467
		/*
		 * boundsObject.x1*=1000; boundsObject.x2*=1000; boundsObject.y1*=1000;
		 * boundsObject.y2*=1000; boundsObject.z1*=1000; boundsObject.z2*=1000;
		 */
		boundsScan.offset(dimGrid * 2f);
	}
	
	
	public void setupGuiLayers() {
		int py = 20;
		int pyO = 10;
		int x1 = 10;
		int x2 = 30;
		gui = new Gui(this, this.createFont("Arial", 12));
		gui.addGuiLabel(x1, py + pyO, "x1x2: ");
		sliderY = gui.addGuiSpinnerGroup(x2, py, 1, 0,  gridHD.nY,4, 0);
		
	}
	
	public void draw() {
		int newY=sliderY.getValue();
		if (newY!=currentY){
			currentY=newY;
			layer=layerManager.loadPlanes(currentY,currentY+1);
			
			for (int i=0;i<img.pixels.length;i++){
				int x=i/img.width;
				int y=i%img.width;
				float v=layer.get(y,0,x);
				if (v<iso){
					img.pixels[i]=WHITE;
				}
				else{
					img.pixels[i]=BLACK;
				}
			}
			img.updatePixels();
			println(currentY);
		}
		drawData();
		drawGui();
	}

	public void drawData() {
		background(0,0,255);
		pushMatrix();
		translate(200,0);
		float scale=height*1f/img.height;
		scale(scale,scale);
		image(img,0,0);
		noFill();
		stroke(255,0,0);
		strokeWeight(gridHD.dim);
		scale(1f/gridHD.dim);
		translate(-boundsScan.x1,-boundsScan.z1);
		rectMode(CORNERS);
		rect(boundsCrop.x1,boundsCrop.z1,boundsCrop.x2,boundsCrop.z2);
		popMatrix();
	}

	public void drawGui() {
		fill(10);
		stroke(100);
		rect(0, 0, 100, height);
		gui.display(g);
	}

}
